{% extends "cpanel/base.html" %}
{% load role_named %}
{% block cpanel_page_title %}用户账户管理{% endblock %}
{% block cpanel_page_body %}
    <ul class="pagination">
        <li><a href="javascript:void(0)" title="添加新账户" id="btn_account_new"><span class="glyphicon glyphicon-plus"></span>添加新账户</a></li>
        <li id="btn-bm-resetpwd" class="disabled"><a href="javascript:void(0)" title="批量重置密码"><span class="glyphicon glyphicon-refresh">批量重置密码</span></a></li>
        <li id="btn-bm-delete" class="disabled"><a href="javascript:void(0)" title="批量删除"><span class="glyphicon glyphicon-remove">批量删除</span></a></li>
        <li><a href="javascript:void(0)" id="btn-filter"><span class="glyphicon glyphicon-filter"></span>&nbsp;筛选</a></li>
    </ul>
    {{ pager }}
    <form action="" id="account_list_form" method="post">
        <div class="table-responsive">
            <table class="table table-bordered table-striped">
                <thead>
                    <tr>
                        <th><input type="checkbox" id="select-all" /></th>
                        <th>ID</th>
                        <th>昵称</th>
                        <th>真实姓名</th>
                        <th>身份</th>
                        <th>邮箱</th>
                        <th>管理</th>
                    </tr>
                </thead>
                <tbody>
                {% for account in account_list %}
                <tr>
                    <td><input type="checkbox" class="batch-manage-checkbox" value="{{ account.id }}" name="account_ids"/></td>
                    <td>
                        <a href="{% url "account_space" account.id %}" target="_blank">{{ account.id }}</a>
                        {% if account.locked %}<span class="label label-danger">已锁定</span>{% endif %}
                    </td>
                    <td>{{ account.nickname }}</td>
                    <td>{{ account.realname }}</td>
                    <td>{{ account.role | role_named }}</td>
                    <td>{{ account.email | default:"" }}</td>
                    <td>
                        <a class="label label-primary btn_account_editor" data-id="{{ account.id }}">编辑</a>
                    </td>
                </tr>
                {% empty %}
                    <tr>
                        <td colspan="7">暂时还没有用户</td>
                    </tr>
                {% endfor %}
                </tbody>
            </table>
        </div>
    </form>
    {{ pager }}
{% endblock %}
{% block cpanel_page_script %}
    <script type="text/javascript">
    $(function () {
       var $bm_form = $("#account_list_form");
        $bm_form.find(".batch-manage-checkbox").change(function () {
           if($bm_form.find(".batch-manage-checkbox:checked").length > 0){
               $("#btn-bm-resetpwd, #btn-bm-delete").removeClass("disabled");
           }else{
               $("#btn-bm-resetpwd, #btn-bm-delete").addClass("disabled");
           }
        });
        $bm_form.find("#select-all").change(function () {
            $bm_form.find(".batch-manage-checkbox").prop("checked", $(this).prop("checked")).change();
        });
        $("#btn_account_new").click(function () {
            $.ModalBox({
                title: '新建账户',
                mode: "url",
                url: "{% url 'cpanel_accountmgr_new_account' %}",
                loaded: function () {
                    $("#form_account_editor").submit(function () {
                        $.GRestP({
                            responseType: "json",
                            callback: function (flag, entity) {
                                console.log(flag);
                                if(flag){
                                    $.AlertBox({body: "新建账户成功", color: "success", title: "操作成功", callback: function(){window.location.reload();} }).show();
                                }else{
                                    $.AlertBox({body: entity.msg, color: "danger", title: "操作失败" }).show();
                                }
                            }
                        }).submit_form(this);
                        return false;
                    });
                },
                default_btn_action: function () {
                    $("#form_account_editor").submit();
                }
            }).show();
        });
        $(".btn_account_editor").click(function () {
            var id = $(this).attr("data-id");
            $.ModalBox({
                title: '修改账户信息',
                mode: "url",
                url: "{% url 'cpanel_accountmgr_modify_account' '__placeholder__' %}".replace("__placeholder__", id),
                loaded: function () {
                    $("#form_account_editor").submit(function () {
                        $.GRestP({
                            responseType: "json",
                            callback: function (flag, entity) {
                                console.log(flag);
                                if(flag){
                                    $.AlertBox({body: "账户信息修改成功", color: "success", title: "操作成功", callback: function(){$.ModalBox().hide();} }).show();
                                }else{
                                    $.AlertBox({body: entity.msg, color: "danger", title: "操作失败" }).show();
                                }
                            }
                        }).submit_form(this);
                        return false;
                    });
                },
                default_btn_action: function () {
                    $("#form_account_editor").submit();
                }
            }).show();
        });
        $("#btn-bm-resetpwd").click(function () {
            if($(this).hasClass("disabled")) return false;
            $.ConfirmBox({
               "title": "操作确认",
               "body": "确定要执行重置密码操作吗？(重置为123456)",
               "color": "danger",
               callback: function (flag) {
                   if (flag) {
                        $.GRestP({
                            responseType: "json",
                            callback: function (flag, entity) {
                                if(flag){
                                    $.AlertBox({body: entity.msg, color: "success", title: "操作成功", callback: function(){window.location.reload();} }).show();
                                }else{
                                    $.AlertBox({body: entity.msg, color: "danger", title: "操作失败" }).show();
                                }
                            }
                        }).submit_form("#account_list_form",'{% url 'cpanel_accountmgr_reset_password' %}');
                   }
               }
            }).show();
        });
        $("#btn-bm-delete").click(function () {
            if($(this).hasClass("disabled")) return false;
            $.ConfirmBox({
               "title": "操作确认",
               "body": "确定要执行删除操作吗？",
               "color": "danger",
               callback: function (flag) {
                   if (flag) {
                        $.GRestP({
                            responseType: "json",
                            callback: function (flag, entity) {
                                if(flag){
                                    $.AlertBox({body: entity.msg, color: "success", title: "操作成功", callback: function(){window.location.reload();} }).show();
                                }else{
                                    $.AlertBox({body: entity.msg, color: "danger", title: "操作失败" }).show();
                                }
                            }
                        }).submit_form("#account_list_form",'{% url 'cpanel_accountmgr_delete_account' %}');
                   }
               }
            }).show();
        });
        var filter_url="{% url 'cpanel_accountmgr_viewlist_filter' %}?account_id=$1&account_id_type=$2&account_role=$3&desc=$4";
        $("#btn-filter").click(function () {
           $.ModalBox({
                title: '筛选',
                mode: "url",
                url: filter_url.replace("$1", "{{ account_id |default:'' }}").replace("$2", "{{ account_id_type |default:'' }}").replace("$3", "{{ account_role }}").replace("$4", "{{ desc }}"),
                loaded: function () {
                    $("#problem_filter_form").attr("action", "{% url "cpanel_accountmgr_viewlist" %}")
                },
                default_btn_action: function () {
                    $("#account_filter_form").submit();
                }
           }).show();
        });
    });
    </script>
{% endblock %}